package com.example.musictutor.repository.facade.impl;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.musictutor.conf.Constants;
import com.example.musictutor.domain.Note;
import com.example.musictutor.domain.Song;
import com.example.musictutor.domain.SongNote;
import com.example.musictutor.factories.SongFactory;
import com.example.musictutor.factories.SongNoteFactory;
import com.example.musictutor.repository.DBHelper;
import com.example.musictutor.repository.facade.DAOFacade;

public class SongDAOFacade implements DAOFacade<Song>{

	private SQLiteDatabase database;
	private DBHelper dbAdapter;
	private Context context;
	
	public SongDAOFacade(Context context)
	{
		this.context = context;
		dbAdapter = new DBHelper(context);
	}
	
	public void open()
	{
		database = dbAdapter.getWritableDatabase();
	}

	public void close()
	{
		dbAdapter.close();
	}
	
	
	@Override
	public Song findEntityById(long id) {
		open();
		Cursor cursor = database.query(Constants.TABLE_SONG, new String[] { Constants.C_ID,
				Constants.C_NAME, Constants.C_TEMPO, Constants.C_HIGH_SCORE, Constants.C_UNLOCKED }, Constants.C_ID + "=?",
	            new String[] { String.valueOf(id) }, null, null, null, null);
	    if (cursor != null)
	        cursor.moveToFirst();
	 
	    SongNoteDAOFacade dao = new SongNoteDAOFacade(context);
	    List<SongNote> songNotes = dao.findEntitiesBySong(id);
	    
	    Song entity = SongFactory.createSong(id, cursor.getString(1), cursor.getInt(2), cursor.getInt(3), (cursor.getInt(4) == 1), songNotes);
	    close();
	    return entity;
	}

	@Override
	public List<Song> getEntities() {
		open();
		List<Song> list = new ArrayList<Song>();

	    String selectQuery = "SELECT  * FROM " + Constants.TABLE_SONG;
	    Cursor cursor = database.rawQuery(selectQuery, null);
	 
	    Song entity;
	    SongNoteDAOFacade dao = new SongNoteDAOFacade(context); 
	    List<SongNote> notes = new ArrayList<SongNote>();
	    if (cursor.moveToFirst()) {
	        do {
	        	notes = dao.findEntitiesBySong(cursor.getInt(0));
	        	entity = SongFactory.createSong(cursor.getInt(0), cursor.getString(1), cursor.getInt(2), cursor.getInt(3), (cursor.getInt(4) == 1), notes);
	            list.add(entity);
	        } while (cursor.moveToNext());
	    }
	    
	    close();
	    return list;  
	}

	@Override
	public void deleteEntity(long id) {
		open();

	    database.delete(Constants.TABLE_SONG, Constants.C_ID + " = ?",
	            new String[] { String.valueOf(id) });
	    close();
	}

	@Override
	public void updateEntity(Song entity) {
		open();
		
		int unlockValue = 0;
		if(entity.isUnlocked())
			unlockValue = 1;
		
		ContentValues values = new ContentValues();
	    values.put(Constants.C_NAME, entity.getName());
	    values.put(Constants.C_TEMPO, entity.getTempo());
	    values.put(Constants.C_HIGH_SCORE, entity.getHighscore());
	    values.put(Constants.C_UNLOCKED, unlockValue);
	 
	    database.update(Constants.TABLE_SONG, values, Constants.C_ID + " = ?",
	            new String[] { String.valueOf(entity.getId()) });
	    close();
	}

	@Override
	public long createEntity(Song entity) {
		open();
		
		int unlockValue = 0;
		if(entity.isUnlocked())
			unlockValue = 1;
		
		ContentValues values = new ContentValues();
	    values.put(Constants.C_NAME, entity.getName());
	    values.put(Constants.C_TEMPO, entity.getTempo());
	    values.put(Constants.C_HIGH_SCORE, entity.getHighscore());
	    values.put(Constants.C_UNLOCKED, unlockValue);
	    
	    long id = database.insert(Constants.TABLE_SONG, null, values);
	    
	    SongNoteDAOFacade dao = new SongNoteDAOFacade(context);
	    for(SongNote note : entity.getNotes()) {
	    	note.setSongId(id);
	    	dao.createEntity(note);
	    }
	    
	    close();
		return id;
	}
	
	public void deleteAll() {
		open();
		database.execSQL("delete from " + Constants.TABLE_SONG);
	    close();
	}

}
